package com.ruoyi.controller;

import com.ruoyi.api.ThirdPartyApi;
import com.ruoyi.api.factory.ThirdPartyApiFactory;
import com.ruoyi.api.resp.ApiResponse;
import com.ruoyi.entity.WeatherData;
import com.ruoyi.feign.NtfyServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/gd/api/weather")
public class WeatherController {

    @Autowired
    private ThirdPartyApiFactory thirdPartyApiFactory; // �����ӿ�

    @Autowired
    NtfyServiceClient ntfyServiceClient;

    @GetMapping("/getData")
    public ResponseEntity<WeatherData> getRealtimeTemperature(@RequestParam String code) {
        ThirdPartyApi gdWeatherApi = thirdPartyApiFactory.createGdWeatherApi();
        ApiResponse<WeatherData> apiResponse = gdWeatherApi.getWeatherData(code);

        if (apiResponse.isSuccess()) {
            return ResponseEntity.ok(apiResponse.getData());
        } else {
            return ResponseEntity.status(HttpStatus.valueOf(Integer.parseInt(apiResponse.getCode())))
                    .body(apiResponse.getData());
        }
    }


    @GetMapping(value = "/getTemperature",produces = "application/json")
    public ResponseEntity<String> getTemperature() {
        String s = ntfyServiceClient.sendTemperature();
        System.out.println(s);
        return ResponseEntity.ok(s);
    }
}
